﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tamir.SharpSsh;
using System.Collections;
using System.Reflection;
using System.IO;

///Author: Asad Fattahi. 02.04.2012
///SharpSSH is a Third party software written by: Tamir Gal
///http://www.tamirgal.com/blog/page/Main.aspx
///Version using in this code is: v1.1.4322
///Three DLL files are required "DiffieHellman.dll, Org.Mentalis.Security.dll, Tamir.SharpSSH"
///
///
///

namespace Cnfi.Core
{
    public class CnfiSftp
    {
        string _host;
        string _username;
        string _password;
        int _port;
        object _sftp;
        Tamir.SharpSsh.jsch.KeyPair _keypair;
        public CnfiSftp(string host, string username, string password, int port, bool connect = true)
        {
            this._host = host;
            this._username = username;
            this._password = password;
            this._port = port;

            //Assembly me = Assembly.GetExecutingAssembly();
            //FileInfo fi = new FileInfo(me.Location);
            //Assembly assembly = Assembly.LoadFile(fi.Directory + @"\Tamir.SharpSSH.dll");

            try
            {
                _sftp = new Sftp(host, username, password);
                if (connect)
                    ((Sftp)_sftp).Connect(port);
            }
            catch (Exception ex)
            {

                string errorString = ex.Message;
                throw ex;
            }

        }

        //Waiting for 3. part SFTP
        //public FenSftp(string host, string username, int port, string publicKey, string passphrase = null,bool connect = true, string sftpDirectoryPath = null, string localDirectoryPath = null)
        //{
        //    this._host = host;
        //    this._username = username;
        //    this._port = port;
        //    try
        //    {
        //        _sftp = new Sftp(host, username);

        //        if (!string.IsNullOrWhiteSpace(passphrase))
        //            AddIdentityFile(publicKey, passphrase);
        //        else
        //        AddIdentityFile(publicKey);
        //        if(connect)
        //        ((Sftp)_sftp).Connect(port);
        //    }
        //    catch (Exception ex)
        //    {

        //        string errorString = ex.Message;
        //        throw ex;
        //    }


        //}
        public void SftpConnect(int port)
        {
            if (!((Sftp)_sftp).Connected)
                ((Sftp)_sftp).Connect(port);
        }

        public void SftpClose()
        {
            if (((Sftp)_sftp).Connected)
                ((Sftp)_sftp).Close();
        }
        public void Put(string fromFile, string toDirectory)
        {
            if (((Sftp)_sftp).Connected)
            {
                ((Sftp)_sftp).Put(fromFile, toDirectory);
            }
        }

        public ArrayList GetFileList(string directory)
        {
            ArrayList fileList = null;
            if (((Sftp)_sftp).Connected)
                fileList = ((Sftp)_sftp).GetFileList(directory);
            return fileList;
        }

        public void AddIdentityFile(string privateKeyfile)
        {
            ((Sftp)_sftp).AddIdentityFile(privateKeyfile);
        }

        public void AddIdentityFile(string privateKeyfile, string passphrase)
        {
            ((Sftp)_sftp).AddIdentityFile(privateKeyfile, passphrase);
        }
        public void MkDir(string directory)
        {
            if (((Sftp)_sftp).Connected)
                ((Sftp)_sftp).Mkdir(directory);
        }

        public void Cancel()
        {
            if (((Sftp)_sftp).Connected)
                ((Sftp)_sftp).Cancel();
        }

        public bool Equals(object obj)
        {
            if (((Sftp)_sftp).Connected)
                return ((Sftp)_sftp).Equals(obj);
            else
                return false;
        }
        public void Get(string fromfilePath)
        {
            if (((Sftp)_sftp).Connected)
                ((Sftp)_sftp).Get(fromfilePath);
        }

        public void Get(string[] fromfilePath)
        {
            if (((Sftp)_sftp).Connected)
                ((Sftp)_sftp).Get(fromfilePath);
        }
        public void Get(string fromfilePath, string toFilePath)
        {
            if (((Sftp)_sftp).Connected)
                ((Sftp)_sftp).Get(fromfilePath, toFilePath);
        }

        public string ToString()
        {
            if (((Sftp)_sftp).Connected)
                return ((Sftp)_sftp).ToString();
            else
                return null;
        }

        #region Properties
        public string Host
        {
            get { return ((Sftp)_sftp).Host; }
        }

        public string Username
        {
            get { return ((Sftp)_sftp).Username; }
        }
        public int Port
        {
            get { return ((Sftp)_sftp).Port; }
        }
        public string ClientVersion
        {
            get { return ((Sftp)_sftp).ClientVersion; }
        }
        public bool Connected
        {
            get { return ((Sftp)_sftp).Connected; }
        }

        public string ServerVersion
        {
            get { return ((Sftp)_sftp).ServerVersion; }
        }

        public string Cipher
        {
            get { return ((Sftp)_sftp).Cipher; }
        }


        #endregion
    }
}


